package com.base.controller.utils;

import com.base.service.CollectCommodityService;
import com.base.utils.RedisConnectionPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

/**
 * 商品收藏工具类
 */
@Component
public class CollectCommodityUtil {

    @Autowired
    private CollectCommodityService collectCommodityService;

    /**
     * 判断用户是否收藏该商品
     * @param uId
     * @param comId
     * @param parentId
     * @return
     */
    public boolean judgeIsCollectCommodity(String uId,String comId,String parentId){
        Jedis jedis = RedisConnectionPool.getJedis();
       try{
           //判断在数据库中是否存在
           boolean database = collectCommodityService.hasCommodityBy(uId, comId, parentId);
           System.out.println("数据库中"+database);
           //判断在redis中是否存在
           boolean hexists = jedis.hexists((uId+"*").getBytes(), (comId+"*").getBytes());
           System.out.println("redis中"+hexists);
           return database || hexists;
       }catch (Exception e){
           e.printStackTrace();
           return  false;
       }finally {
           RedisConnectionPool.returnResource(jedis);
       }
    }

}
